package com.example.rabbitmqboot.controller;

import com.alibaba.fastjson.JSON;
import com.example.rabbitmqboot.pojo.Student;
import org.springframework.amqp.core.Correlation;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.UUID;

@RestController
public class RabbitSender {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("/send/{msg}")
    private String send(@PathVariable("msg") String msg) {
        //1、消息配置
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setAppId(UUID.randomUUID().toString());
        messageProperties.setContentType("application/text");
        messageProperties.setContentEncoding("UTF-8");

        //2、消息
        Message message = new Message(JSON.toJSONBytes(msg), messageProperties);

        //3、设置回调信息
        ReturnedMessage returnedMessage =
                new ReturnedMessage(message, 200, "回调信息", "SchoolExchange", "homework.match");

        //4、确认回调
        CorrelationData correlationData = new CorrelationData();
        correlationData.setReturned(returnedMessage);
        correlationData.setId(UUID.randomUUID().toString());

        //5、发送数据
        rabbitTemplate.convertAndSend("SchoolExchange", "homework.match", msg, correlationData);

        return "发送成功";
    }

    @RequestMapping("/send2")
    private String send2() {
        //1、消息配置
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setAppId(UUID.randomUUID().toString());
        messageProperties.setContentType(MessageProperties.CONTENT_TYPE_JSON);
        messageProperties.setContentEncoding("UTF-8");

        for (int i = 0; i < 10; i++) {
            Student student = new Student()
                    .setId(UUID.randomUUID().toString())
                    .setName("学生" + i)
                    .setContent("hhh");

            //2、消息
            Message message = new Message(JSON.toJSONBytes(student), messageProperties);

            //3、设置回调信息
            ReturnedMessage returnedMessage = new ReturnedMessage(message, 200, "回调信息2", "StudentExchange", "student.match");

            //4、确认回调
            CorrelationData correlationData = new CorrelationData();
            correlationData.setReturned(returnedMessage);

            //5、发送数据
            rabbitTemplate.convertAndSend("StudentExchange", "Student.#", message, correlationData);
        }


        return "发送成功";
    }

    /* 发送JSON消息使用不同转换器接收 */
    @RequestMapping("send3")
    private void moreTypeSend() throws IOException {

        MessageProperties properties = new MessageProperties();
        properties.setAppId(UUID.randomUUID().toString());
        properties.setContentEncoding("UTF-8");


        /* 发送图片消息 */
        byte[] body = Files.readAllBytes(Paths.get("D:\\img", "ee04a05a-1230-46b6-8ad5-1a95b140fff3.png"));
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setContentType("image");
        Message message = new Message(body, messageProperties);

        /* 回调消息 */
        ReturnedMessage reMsg = new ReturnedMessage(message,
                200, "这是send2传递的消息",
                "TeacherExchange", "Teacher.info");
        CorrelationData correlationData = new CorrelationData();
        correlationData.setReturned(reMsg);


        rabbitTemplate.convertAndSend("StudentExchange", "File.picinfo", message, correlationData);

        /* 发送PDF消息 */
        byte[] body2 = Files.readAllBytes(Paths.get("D:\\img", "梁浩-java后端开发.pdf"));
        MessageProperties messageProperties2 = new MessageProperties();
        messageProperties2.setContentType("application/pdf");
        Message message2 = new Message(body2, messageProperties2);
        rabbitTemplate.convertAndSend("StudentExchange", "File.pdfinfo", message2, correlationData);


    }

}
